package cn.tide.question2.controller;

import cn.tide.question2.dao.PersonDao;
import cn.tide.question2.domain.Hobby;
import cn.tide.question2.domain.Person;
import cn.tide.question2.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("")
public class PersonController {

    @Autowired
    PersonService personService;

    @RequestMapping("/q2Add")
    public String add(Person person, MultipartFile image,HttpServletRequest request){

        System.out.println(person+",,,");
        Iterator<Hobby> it = person.getHobby().iterator();
        while(it.hasNext()){
            String name = it.next().getName();
            if(name==null){
//                System.out.println(name);
                it.remove();
            }
        }

//        File imageFolder= new File(request.getSession().getServletContext().getRealPath("img/person"));
//        File file = new File(imageFolder,person.getId()+".jpg");
//        if(!file.getParentFile().exists())
//            file.getParentFile().mkdirs();
////        System.out.println("cccp:"+person);
        try {
            personService.add(person);
//            image.transferTo(file);
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }

        File imageFolder= new File(request.getSession().getServletContext().getRealPath("img/person"));
//        System.out.println(imageFolder);
        File file = new File(imageFolder,person.getId()+".jpg");
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        try {
            image.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/html/list.html";
    }

    @RequestMapping("/q2AddPic")
    public void addPic(MultipartFile image,HttpServletRequest request){
//                File imageFolder= new File(request.getSession().getServletContext().getRealPath("img/person"));
//        File file = new File(imageFolder,person.getId()+".jpg");
//        if(!file.getParentFile().exists())
//            file.getParentFile().mkdirs();
//
//        image.transferTo(file);
    }

    @RequestMapping("/q2Select")
    public String select(int id){

        Person person = personService.findById(id);

        System.out.println(person);
        if (null == person){
            return "fail";
        }
        return "success";
    }

    @RequestMapping("/q2SelectAll")
    @ResponseBody
    public Object selectAll(){
        List<Person> personList = personService.findAll();
//        System.out.println(personList);

        return personList;
    }

    @RequestMapping("/q2Delete")
    public String delete(int id, HttpSession session){
        try {
            personService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }

        File  imageFolder= new File(session.getServletContext().getRealPath("img/person"));
        File file = new File(imageFolder,id+".jpg");
        file.delete();

        return "redirect:/html/list.html";
    }

    @RequestMapping("/q2Update")
    public String update(Person person, MultipartFile image,HttpServletRequest request){
        Iterator<Hobby> it = person.getHobby().iterator();
        while(it.hasNext()){
            String name = it.next().getName();
            if(name==null){
//                System.out.println(name);
                it.remove();
            }
        }

        try {
            personService.update(person);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }

        File imageFolder= new File(request.getSession().getServletContext().getRealPath("img/person"));
//        System.out.println(imageFolder);
        File file = new File(imageFolder,person.getId()+".jpg");
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        try {
            image.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/html/list.html";
    }

    @RequestMapping("/toUpdate")
    @ResponseBody
    public Object toUpdate(String id){
//        System.out.println(id);
        Person person = personService.findById(Integer.parseInt(id));
//        System.out.println(person);

        return person;
    }
}
